package com.ssm.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.ssm.bean.MaterialInfo;
import com.ssm.dao.MaterialBaseInfoMapper;
import com.ssm.service.MaterialInfoService;
import com.ssm.util.ExcelException;
import com.ssm.util.ExcelUtil;
import com.ssm.util.SystemConfigUtil;

/**
 * @author ZhaoXianming
 * @Description 
 * @date 2020年3月16日
 */
@Service
public class MaterialInfoServiceImpl implements MaterialInfoService
{
	Logger log = LoggerFactory.getLogger(MaterialInfoServiceImpl.class);
	
    @Autowired
    private MaterialBaseInfoMapper materialBaseInfoMapper;

    @Override
    public int selectAllMaterialInfoCount()
    {
        return materialBaseInfoMapper.selectAllMaterialInfoCount();
    }

    /**
     * 新增物料
     */
    @Override
    public void insertOrUpdateMaterialInfo(MaterialInfo materialInfo)
    {
        materialBaseInfoMapper.insertOrUpdateMaterialInfo(materialInfo);
    }

    @Override
	public void deleteRfidCard(String materialCode) {
    	materialBaseInfoMapper.deleteRfidCard(materialCode);
	}
    
    @Override
    public void bindRfidCard(String materialCode, String rfidCardNumber) {
    	materialBaseInfoMapper.bindRfidCard(materialCode, rfidCardNumber);
    }
    
    /**
     * 删除物料基础信息
     */
    @Transactional
    @Override
    public String deleteMaterialBaseInfo(List<String> delList) throws Exception
    {
    	try{
    		materialBaseInfoMapper.deleteMaterialBaseInfo(delList);
    		return "success";
    	}catch(Exception e){
    		log.error("删除异常", e);
    		throw new RuntimeException();
    	}
    }

    @Override
    public void updateMaterialInfo(MaterialInfo materialInfo)
    {
        materialBaseInfoMapper.updateMaterialInfo(materialInfo);
    }

    @Override
    public MaterialInfo selectMaterialByCode(String materialCode)
    {
        return this.materialBaseInfoMapper.selectMaterialByCode(materialCode);
    }

    /**
     * 根据物料编码判断物料是否重复
     */
    @Override
    public boolean isMaterialRepeat(String materialCode)
    {
        return materialBaseInfoMapper.isMaterialRepeat(materialCode);
    }
    
    /**
     * 根据物料编码判断物料是否重复
     */
    @Override
    public boolean isRfidCardNumberRepeat(String rfidCardNumber)
    {
    	return materialBaseInfoMapper.isRfidCardNumberRepeat(rfidCardNumber);
    }

	@Override
	public List<MaterialInfo> selectTopMaterialInfoByParams(
			Map<String, Object> params) {
		return materialBaseInfoMapper.selectTopMaterialInfoByParams(params);
	}

    @Override
    public List<String> selectMaterialByParam(String materialName,
            String materialSpec, String materialModel)
    {
        return materialBaseInfoMapper.selectMaterialByParam(materialName, materialSpec, materialModel);
    }

	@Override
	public List<MaterialInfo> selectAllMaterialInfoWithDev(Map<String,Object> params) {
		return materialBaseInfoMapper.selectAllMaterialInfoWithDev(params);
	}

	@SuppressWarnings("unchecked")
	@Transactional
    public void saveFileToMysql(MultipartFile file) throws ExcelException
    {
        log.info("--------进入了本地原有物料的Excel导入的方法--------");
        List<MaterialInfo> instockDetailss = new ArrayList<>();
        List<MaterialInfo> instockDetails = new ArrayList<>();
        InputStream isInputStream = null;
        try
        {
            // 创建输入流
            isInputStream = file.getInputStream();
            log.info("--------获取excel中文名和 属性键值对--------");
            // 获取excel中文名和 属性键值对
            Map<String, String> proMap = SystemConfigUtil.getInstance()
                    .getAppointProMap("materialExcelModel");
            log.info("--------Excel导入的时候获取配置文件中的属性个数："+proMap.size());
            // 获取数据
            instockDetailss = (List<MaterialInfo>) ExcelUtil.getListFromExcel(
                    isInputStream, MaterialInfo.class, proMap);
            // 去掉了入库的时候为NUll的数据
            for (int i = 0; i < instockDetailss.size(); i++)
            {
                if (instockDetailss.get(i).getMaterialCode() != null)
                {
                    instockDetails.add(instockDetailss.get(i));
                }
            }
            materialBaseInfoMapper.insertByExcel(instockDetails);
        }
        catch(DuplicateKeyException e){
        	log.error("文件中已存在物料信息，请确认！", e);
        	throw new ExcelException();
        }
        catch(Exception e)
        {
        	log.error("导入文件出错了", e);
            throw new ExcelException();
        } finally
        {
            try
            {
            	if(isInputStream!=null){
            		isInputStream.close();
            	}
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
    }

}
